﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Diagnostics;
using SharpSvn;
using System.IO;
using System.Threading;

namespace Updater
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly BackgroundWorker worker = new BackgroundWorker();
        private readonly BackgroundWorker workerTimer = new BackgroundWorker();
        string filesource = "Z:\\install\\MAC OS\\VMware Workstation 8.0.1\\VMware-workstation-full-8.0.1-528992.exe";
        string filedest = "c:\\VMware-workstation-full-8.0.1-528992.exe";
        string appname = "";
        string spath = "";
        string updates = "";
        string exepath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
        SvnClient svn_client = new SvnClient();
        int pr = 0;

        public MainWindow()
        {
            InitializeComponent();
            worker.DoWork += worker_DoWork;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.ProgressChanged += worker_ProgressChanged;
            //worker.WorkerReportsProgress = true;
            workerTimer.DoWork += workerTimer_DoWork;
            workerTimer.ProgressChanged += workerTimer_ProgressChanged;
            workerTimer.WorkerReportsProgress = true;
            string inipath = exepath + "\\Settings.ini";
            IniParser parser = new IniParser(inipath);
            appname = parser.GetSetting("Main", "AppName");
            spath = parser.GetSetting("Main", "SourcePath");
            updates = parser.GetSetting("Main", "Updates");
            //IniFile inif=new IniFile(inipath);
            //appname=inif.Read("AppName", "Main");
            //spath = inif.Read("SourcePath", "Main");
            //updates = inif.Read("Updates", "Main");
            //svn_client.Progress += new EventHandler<SvnProgressEventArgs>(UpdateProgressBar);
            statustext.Text = "Atnaujinama...";
        }

        private void progressBar1_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            workerTimer.RunWorkerAsync();
            worker.RunWorkerAsync();
            
        }


        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
           /* foreach (string upd in updates.Split(';'))
            {
                string src = spath + "\\" + upd;
                XCopy.Copy(src, exepath+"\\"+upd, true, true, (o, pce) =>
                {
                    worker.ReportProgress(pce.ProgressPercentage, src);
                });

            }*/
            //System.Collections.ObjectModel.Collection<SvnListEventArgs> contents=new System.Collections.ObjectModel.Collection<SvnListEventArgs>();
           /* SvnTarget st=new Uri("https://advertising-lrtv.googlecode.com/svn/trunk/ReklamaWPF/bin/Release");
            svn_client.GetList(st, out contents);
            int cnt = contents.Count;
            progressBar1.Maximum = cnt;
            int i=1;
            foreach (SvnListEventArgs item in contents)
            {
                //FileInfo object and get the Length.
             // FileInfo fi = new FileInfo(item.Path);
              //long fileLength = fi.Length;
              //fi = null;
                i++;
                //if (i == cnt) { }
            }*/

            try{
            svn_client.CheckOut(new Uri(spath), exepath + "\\exec"); 
            }catch(Exception ex){
                }
        }

        private void worker_RunWorkerCompleted(object sender,
                                               RunWorkerCompletedEventArgs e)
        {
            Process pr = new Process();
            Process.Start(exepath + "\\exec\\" + appname);
            Close();
        }

        private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //progressBar1.Value = e.ProgressPercentage;
            /*int totalprog = Int32.Parse(e.UserState.ToString());
            if (totalprog != -1)
            {
                progressBar1.Maximum = totalprog;
            }
            if (e.ProgressPercentage > totalprog) progressBar1.Maximum = e.ProgressPercentage + 1;
            progressBar1.Value = (int)e.ProgressPercentage;
            if (progressBar1.Value < 10) { Thread.Sleep(200); }*/
            
        }

        private void workerTimer_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                pr=pr+10;
                if (pr>=100) {pr=0;}
                workerTimer.ReportProgress(pr);
                Thread.Sleep(200);
            }
        }

        private void workerTimer_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void UpdateProgressBar(object sender, SvnProgressEventArgs e)
        {
            worker.ReportProgress((int)e.Progress, e.TotalProgress.ToString());
            

        }
    }
}
